package com.intretech.umsin.code.generator.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * @version V1.0
 * @ClassName: SchemaHelper
 * @Description:(Schema 帮助类)
 * @author: 李启联
 * @date: 2020-01-22 09:54
 * @Copyright: Copyright (c), 2020
 */
@Component
public class SchemaHelper {

    @Autowired
    private DataSource dataSource;

    /**
     * 得到当前连接的数据库类型
     * @return 数据库类型
     * @throws Exception
     */
    public  DataBaseType getDataBaseType() throws Exception {
        String dataBaseType = dataSource.getConnection().getMetaData().getDatabaseProductName();
        if(dataBaseType.toUpperCase().contains("MYSQL"))
            return Enum.valueOf(DataBaseType.class, "MYSQL");
        else if(dataBaseType.toUpperCase().contains("MICROSOFT SQL SERVER"))
            return Enum.valueOf(DataBaseType.class, "SQLSERVER");
        else if(dataBaseType.toUpperCase().contains("ORACLE"))
            return Enum.valueOf(DataBaseType.class, "ORACLE");
        else
            return Enum.valueOf(DataBaseType.class, "UNKNOWN");
    }


}